namespace FluentBinding.WinForms
{
    internal class PropertyAndTypeConvertorAdapter<E, TSelected, P> : IDataMemberConvertor<E>
    {
        #region Fields

        private readonly IPropertyConvertor<E, TSelected> _propertyConvertor;
        private readonly ITypeConvertor<TSelected, P> _typeConvertor;

        #endregion

        #region Constructors

        public PropertyAndTypeConvertorAdapter(IPropertyConvertor<E, TSelected> propertyConvertor,
                                               ITypeConvertor<TSelected, P> typeConvertor)
        {
            _propertyConvertor = propertyConvertor;
            _typeConvertor = typeConvertor;
        }

        #endregion

        #region IDataMemberConvertor<E> Members

        public object Select(E entity)
        {
            return _typeConvertor.Convert(_propertyConvertor.Select(entity));
        }

        public void Initialize(E entity, object value)
        {
            _propertyConvertor.Initialize(entity, _typeConvertor.Convert((P)value));
        }

        #endregion
    }
}